package cn.gzmli.dtt.common.workflow.controller;

import cn.gzmli.dtt.common.annotation.Comment;
import cn.gzmli.dtt.common.entity.PageInfo;
import cn.gzmli.dtt.common.entity.Result;
import cn.gzmli.dtt.common.utils.BaseUtil;
import cn.gzmli.dtt.common.utils.Constant;
import cn.gzmli.dtt.common.workflow.entity.FlowNodeMessage;
import cn.gzmli.dtt.common.workflow.service.FlowNodeMessageService;
import org.hibernate.criterion.*;
import org.hibernate.mapping.Collection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Descript 节点消息相关接口
 * @Author xsx
 * @Date 2018/1/7
 */
@RestController
@RequestMapping("/flowNodeMessage")
public class FlowNodeMessageController {

    @Autowired
    private FlowNodeMessageService flowNodeMessageService;
    @RequestMapping("/list")
    @ResponseBody
    @Comment("获取节点消息列表")
    public Result list() throws Exception {
        return new Result(1, "获取节点消息列表成功", this.flowNodeMessageService.getAll());
    }

    @RequestMapping("/listByCondition")
    @ResponseBody
    @Comment("获取节点消息列表")
    public Result listByCondition(HttpServletRequest request, PageInfo pageInfo,
                                  Integer flowNodeId,
                                  Integer eventType,
                                  Integer tStatus
                                  ) throws Exception {

        DetachedCriteria condition = DetachedCriteria.forClass(FlowNodeMessage.class);

        //添加流程节点查询条件
        if(BaseUtil.isNotEmpty(flowNodeId)){
            condition.add(Restrictions.eq("flowNode.id",flowNodeId));
        }
        //添加节点事件查询条件
        if(BaseUtil.isNotEmpty(eventType)){
            switch (eventType){
                case Constant.WorkFlow.WORKFLOW_MESSAGE_EVENTTYPE_RECIVE:
                    condition.add(Restrictions.eq("reciveState",1));
                    break;
                case Constant.WorkFlow.WORKFLOW_MESSAGE_EVENTTYPE_PROC:
                    condition.add(Restrictions.eq("dealState",1));
                    break;
                case Constant.WorkFlow.WORKFLOW_MESSAGE_EVENTTYPE_SEND:
                    condition.add(Restrictions.eq("sendState",1));
                    break;
            }
        }
        //添加任务状态查询条件
        if(BaseUtil.isNotEmpty(tStatus)){
          /*  if(tStatus.equalsIgnoreCase("NOTNULL")){
                condition.add(Restrictions.isNotEmpty("tlStatus"));
            }else if(tStatus.equalsIgnoreCase("NULL")){
                condition.add(Restrictions.isEmpty("tlStatus"));
            }else {*/
                condition.add(Restrictions.eq("tStatus",tStatus));
           //}
        }
       /* //添加任务记录状态查询条件
        if(BaseUtil.isNotEmpty(tlStatus)){
            Object [] inConds= null;
                if(tlStatus.indexOf(",")!=-1){
                    inConds=tlStatus.split(",");
                }else{
                    inConds=new Object[]{tlStatus};
                }
                condition.add(Restrictions.in("tlStatus",inConds));
        }*/

        condition.addOrder(Order.asc("workFlow.id"));
        List<FlowNodeMessage> data = this.flowNodeMessageService.getByCondition(condition, pageInfo);
        return new Result(1, "获取节点消息列表成功", data, pageInfo);
    }

    @RequestMapping("/get")
    @ResponseBody
    @Comment("获取节点消息信息")
    public Result get(Integer id) throws Exception {
        return new Result(1, "获取节点消息信息成功", this.flowNodeMessageService.get(id));
    }

    @RequestMapping("/getByCondition")
    @ResponseBody
    @Comment("获取节点消息信息")
    public Result getByCondition(HttpServletRequest request) throws Exception {
        Map<String, String> condition = new HashMap<>();
        return new Result(1, "获取节点消息信息成功", this.flowNodeMessageService.getByCondition(condition));
    }

    @RequestMapping("/save")
    @ResponseBody
    @Comment("保存节点消息信息")
    public Result save(FlowNodeMessage flowNodeMessage) throws Exception {
        return new Result(1, "保存节点消息信息成功", this.flowNodeMessageService.save(flowNodeMessage));
    }

    @RequestMapping("/update")
    @ResponseBody
    @Comment("更新节点消息信息")
    public Result update(FlowNodeMessage flowNodeMessage) throws Exception {
        this.flowNodeMessageService.update(flowNodeMessage);
        return new Result(1, "更新节点消息信息成功");
    }

    @RequestMapping("/delete")
    @ResponseBody
    @Comment("删除节点消息信息")
    public Result delete(FlowNodeMessage flowNodeMessage) throws Exception {
        this.flowNodeMessageService.delete(flowNodeMessage);
        return new Result(1, "删除节点消息信息成功");
    }

    @RequestMapping("/updateStatus")
    @ResponseBody
    @Comment("变更节点消息状态")
    public Result updateStatus(Integer id, Integer status) throws Exception {
        this.flowNodeMessageService.updateStatus(id, status);
        return new Result(1, status==1?"启用节点消息信息成功":"禁用节点消息信息成功");
    }

	public Result lists(HttpServletRequest request, PageInfo pageInfo) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}
}
